﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Repid.Core.Context;

namespace Repid.Core.Aop
{
    public abstract class AopTarget : MarshalByRefObject
    {
        /// <summary>
        /// 数据上下文
        /// </summary>
        internal DataContext db;

        /// <summary>
        /// 前置
        /// </summary>
        internal void PreProceede()
        {
            db = new DataContext();
        }

        /// <summary>
        /// 后置
        /// </summary>
        internal void PostProceede()
        {
            db.Dispose();
        }

        /// <summary>
        /// 反馈
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        internal ServiceResult Result(bool success, string message = null, object data = null)
        {
            return new ServiceResult(success, message, data);
        }

        /// <summary>
        /// 成功反馈
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        internal ServiceResult SuccessResult(string message = "操作成功!", object data = null)
        {
            return Result(true, message, data);
        }

        /// <summary>
        /// 失败反馈
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        internal ServiceResult FailureResult(string message = "操作失败!", object data = null)
        {
            return Result(false, message, data);
        }

        /// <summary>
        /// 未知反馈
        /// </summary>
        /// <param name="success"></param>
        /// <param name="successMessage"></param>
        /// <param name="failureMessage"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        internal ServiceResult UnknownResult(bool success, string successMessage = "操作成功!", string failureMessage = "操作失败!", object data = null)
        {
            if (success)
            {
                return SuccessResult(successMessage, data);
            }
            return FailureResult(failureMessage, data);
        }
    }
}